package com.luoye.audiodemo.weight

import android.content.Context
import android.graphics.BitmapFactory
import android.opengl.GLES20
import android.opengl.GLSurfaceView
import android.util.AttributeSet
import com.blankj.utilcode.util.LogUtils
import com.luoye.audiodemo.R
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStreamReader
import java.nio.ByteBuffer
import java.nio.ByteOrder
import java.nio.FloatBuffer
import java.nio.ShortBuffer
import javax.microedition.khronos.egl.EGLConfig
import javax.microedition.khronos.opengles.GL10

class MyGLView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
) : GLSurfaceView(context, attrs) {
    init {
        setRenderer(YGLRender())
    }
}

class YGLRender : GLSurfaceView.Renderer {
    override fun onSurfaceCreated(p0: GL10?, p1: EGLConfig?) {
        LogUtils.e("创建")
    }

    override fun onSurfaceChanged(p0: GL10?, w: Int, h: Int) {
        GLES20.glViewport(0, 0, w, h)
        LogUtils.e(w,h)
    }

    override fun onDrawFrame(p0: GL10?) {
        LogUtils.e("onDrawFrame")
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT)
        GLES20.glClearColor(1f, 0f, 0f, 1f)
    }
}

class MyGLView1 @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
) : GLSurfaceView(context, attrs),GLSurfaceView.Renderer {
    init {
        setEGLContextClientVersion(2)
        setRenderer(this)
    }

    override fun onSurfaceCreated(p0: GL10?, p1: EGLConfig?) {

    }

    override fun onSurfaceChanged(p0: GL10?, w: Int, h: Int) {
        GLES20.glViewport(0, 0, w, h)
    }

    override fun onDrawFrame(p0: GL10?) {
        GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT)
        GLES20.glClearColor(1f, 0f, 0f, 1f)
    }
}





/**
 * 读取文件
 */
fun readRawTextFile(context: Context, rawId: Int): String {
    val `is` = context.resources.openRawResource(rawId)
    val br = BufferedReader(InputStreamReader(`is`))
    var line: String?
    val sb = StringBuilder()
    try {
        while (br.readLine().also { line = it } != null) {
            sb.append(line)
            sb.append("\n")
        }
    } catch (e: Exception) {
        e.printStackTrace()
    }
    try {
        br.close()
    } catch (e: IOException) {
        e.printStackTrace()
    }
    return sb.toString()
}



